<?
require("/var/www/html/new/d/common/auth.php");
require("/var/www/html/new/d/class/OrderRecord.php");
require("/var/www/html/new/d/class/abc/OrderRecord_gly.php");
$ac=$GLOBALS['_ac'];
$pw=$GLOBALS['_pw'];
$s = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));

$memberID = $tmpMemberID ? $tmpMemberID:$_SESSION['sid'];// $tmpMemberID 二级店铺存在，鼓浪屿取消使用
$errCode = array(
	'118'=>'终端信息出错，请联系技术人员[118]',
	'146'=>'终端信息出错，请联系技术人员[146]',
);
switch($_REQUEST['from']){
	case "order_cancel":
		$ordernum=$_REQUEST['ordernum'];
		$or=new OrderRecord_gly($ordernum,$s);
		// $orders=$or->info();
		$orders=$or->info_n($_SESSION['sid']);
		$concat = $_REQUEST['concat'] ? $_REQUEST['concat'] : 0;
		// 记录访问信息
		include_once("/var/www/html/new/d/class/AccessRecord.php");
		if(AccessRecord::insert(1,"退款|$ordernum|{$_SESSION['memberID']}")){
			$result = array();
			try{
				$result = $or->cancel($memberID,$concat);
			}catch(Exception $e){
				$out=array("outcome"=>-1,"msg"=>$e->getMessage());
				exit(json_encode($out));
			}
			if($result['code']==100){
				$msg="您".$orders['mainOrder']->UUltitle.$orders['mainOrder']->UUttitle."的订单".(string)$orders['mainOrder']->UUordernum."已被取消。【票付通】";
				$s->Send_SMS_System_Fee((string)$orders['mainOrder']->UUordertel,$msg,(string)$orders['mainOrder']->UUaid,(string)$orders['mainOrder']->UUordernum);
				$out=array("outcome"=>1);
				if($_SESSION['dtype']==6){
					$daction="取消订单($ordernum)";
					$str="insert pft_d_operation_rec set fid=".$_SESSION['memberID'].",sid=".$_SESSION['sid'].",rectime=now(),daction='$daction'";
					$GLOBALS['le']->query($str);
				}
			}else{ 
				$out=array("outcome"=>-1,"msg"=>$errCode[$result['msg']]);
			}
		}else{ 
			$out=array("outcome"=>-1,"msg"=>'操作信息记录失败');
		}
		
		echo json_encode($out);
		break;
	case "order_resend":
		$ordernum=$_REQUEST['ordernum'];
		$or=new OrderRecord($ordernum,$s);
		$orders=$or->info();
		if((string)$orders['mainOrder']->UUremsg>=3){
			$out=array("outcome"=>-1,"msg"=>"短信重发次数不能超过2次");
		}else{
			if($or->resendMsg($orders)){
				$out=array("outcome"=>1);
				if($_SESSION['dtype']==6){
					$daction="重发订单短信($ordernum)";
					$str="insert pft_d_operation_rec set fid=".$_SESSION['memberID'].",sid=".$_SESSION['sid'].",rectime=now(),daction='$daction'";
					$GLOBALS['le']->query($str);
				}
			}else{ 
				$out=array("outcome"=>-1);
			}
		}
		echo json_encode($out);
		break;

	 // 订单取消检测
	case 'cancelCheck':
		$ordernum=$_REQUEST['ordernum'];
		$or=new OrderRecord($ordernum,$s);
		$ordersInfo=$or->info();
		$orders = array(
			$ordersInfo['mainOrder']
		);
		$orders = array_merge($orders,$ordersInfo['childOrder']);
		$resArr = array('code'=>100);
		$cost = $mid = 0; // 取消总费用
		if($orders[0]->UUpaystatus!=2){
			foreach($orders as $order){
				// 不算套票的子票
				if((string)$order->UUpack_order>1) continue;
				$mid = (string)$order->UUmid;
				$tid = (string)$order->UUtid;
				$sql = "select reb,reb_type from uu_jq_ticket where id=$tid limit 1";
				$GLOBALS['le']->query($sql);
				$GLOBALS['le']->fetch_assoc();
				$reb = $GLOBALS['le']->f('reb');// 退款数值
				$reb_type = $GLOBALS['le']->f('reb_type');// 退款类型 0 百分比 1 具体指
				
				// 总价
				$tprice = (string)$order->UUtprice;
				$tnum = (string)$order->UUtnum;
				// 是否被修改了，取消按购买的票数
				$ordernum = (string)$order->UUordernum;
				$sql = "select pre_num from pft_order_num_modify where orderid='$ordernum' order by id asc limit 1";
				$GLOBALS['le']->query($sql);
				$GLOBALS['le']->fetch_assoc();
				if($GLOBALS['le']->f('pre_num')) $tnum = $GLOBALS['le']->f('pre_num');
				$total = $tprice * $tnum;
				$cost += ($reb_type==1) ? ($reb / 100 * $tnum) : $total * $reb/10000;
			}
			// 支付宝支付
			$refund_back = false;
			if($orders[0]->UUpmode==1){
				$sourceAid = (string)$orders[0]->UUaids;
				if($sourceAid==0){
					$sourceAid = (string)$orders[0]->UUaid;
				}else{
					$tmp = explode(',', $sourceAid);
					$sourceAid = $tmp[0];
				}
				$sql="select refund_back from pft_member_extinfo where fid=$sourceAid limit 1";
				$GLOBALS['le']->query($sql);
				$GLOBALS['le']->fetch_assoc();
				if($GLOBALS['le']->f('refund_back')){
					$refund_back = true;
				}
			}
			
			if(!$refund_back){
				// 获取分销商账户余额
				$result = $s->PFT_Member_Fund($mid,0);
				$object = simplexml_load_string($result);
				$balance = (string)$object->Rec->UUamoney;
				if($balance<0 && $cost>0){// 为 0 时给取消
					$resArr['code'] = 101;
					$resArr['msg'] = '账户余额不足';
				}
			}
		}

		$resArr['cost'] = $cost;
		$resArr['outcome'] = 1;
		echo json_encode($resArr);
		break;
	default:
		exit("非法操作");

}
@$GLOBALS['le']->close();
exit;
?>